﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Scripting.Hosting;
using JavascriptEngine.Runtime;

namespace JavascriptEngine
{
    public class Host
    {
        ScriptRuntime _runtime;
        ScriptEngine _engine;
        ScriptScope _scope;

        public Host()
        {
            ScriptRuntimeSetup scriptRuntimeSetup = new ScriptRuntimeSetup();
            scriptRuntimeSetup.DebugMode = true;
            scriptRuntimeSetup.LanguageSetups.Add(new LanguageSetup(typeof(JavascriptContext).AssemblyQualifiedName,
                JavascriptContext.JavascriptDisplayName,
                JavascriptContext.JavascriptNames.Split(';'),
                JavascriptContext.JavascriptFileExtensions.Split(';')));

            _runtime = new ScriptRuntime(scriptRuntimeSetup);
            _engine = _runtime.GetEngine("js");
            _scope = _engine.CreateScope();
        }

        public ScriptScope Run(string path)
        {
            return _engine.ExecuteFile(path, _scope);
        }
    }
}
